/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.admin;

import hibernate.dao.EventDAO;
import hibernate.entity.Login;
import hibernate.entity.Event;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import util.Converter;
import util.RandomGenerator;

/**
 *
 * @author NDH
 */
@ManagedBean
@RequestScoped
public class EventManager extends AbstractAdminBean {

//    private int STU_ROLE_ID = 3;
    /**
     * Creates a new instance of StudentManager
     */
    public EventManager() {
    }
    List<Event> listEvent;
    EventDAO eDao = new EventDAO();
    Event currEvent = new Event();
    String stStartDate;
    String stEndDate;

    public String getStStartDate() {
        return stStartDate;
    }

    public void setStStartDate(String stStartDate) {
        this.stStartDate = stStartDate;
    }

    public String getStEndDate() {
        return stEndDate;
    }

    public void setStEndDate(String stEndDate) {
        this.stEndDate = stEndDate;
    }

    public List<Event> getListEvent() {
        listEvent = eDao.readAll();
        return listEvent;
    }

    public void setListEvent(List<Event> listEvent) {
        this.listEvent = listEvent;
    }

    public Event getCurrEvent() {
        return currEvent;
    }

    public void setCurrEvent(Event currEvent) {
        this.currEvent = currEvent;
    }

    public String delete(Event s) {
        try {
            hbTransaction = hbSession.beginTransaction();
            hbSession.delete(s);
            hbTransaction.commit();
        } catch (Exception e) {
            hbTransaction.rollback();
        }
        return "list";
    }

    public String edit(Event s) {
        currEvent = s;
        return "edit";
    }

    void setDate() {
        try {
            currEvent.setStartTime(Converter.stringToDateJava(stStartDate));
            currEvent.setEndTime(Converter.stringToDateJava(stEndDate));
        } catch (ParseException ex) {
            Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void docreate(ActionEvent actionEvent) {
        setDate();
        try {
            if (eDao.create(currEvent) > 0) {
                facesContext.addMessage(null, new FacesMessage("Successful", "Event: " + currEvent.getName() + " created!"));
            }
        } catch (Exception e) {
        }
    }

    public String doupdate() {
        try {
            setDate();
            if (eDao.update(currEvent) > 0) {
                facesContext.addMessage(null, new FacesMessage("Successful", "Event: " + currEvent.getName() + " updated!"));
            }
        } catch (Exception e) {
        }
        return "list";
    }

    public void test(ActionEvent actionEvent) {
        FacesContext context = FacesContext.getCurrentInstance();

        context.addMessage(null, new FacesMessage("Successful", "Hello "));
        context.addMessage(null, new FacesMessage("Second Message", "Additional Info Here..."));
    }
}
